﻿using BlazorShop.Data;
using Microsoft.EntityFrameworkCore;

namespace BlazorShop.Models.SeedData
{
    public class SeedData
    {
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new BlazorShopContext(
                serviceProvider.GetRequiredService<
                    DbContextOptions<BlazorShopContext>>()))
            {
                // Look for any products.
                if (context.Product.Any() && context.User.Any() && context.Category.Any())
                {
                    return;   // DB has been seeded
                }

                context.Product.AddRange(
                    new Product
                    {
                        ProductName = "C#入门经典",
                        ProductDescription = "入门经典系列",
                        Price = 99,
                        ImageUrl = "",
                        CategoryId = 1,
                        ProductType = "Reference"
                    },
                    new Product
                    {
                        ProductName = "时间简史",
                        ProductDescription = "霍金时间简史带你穿越时间线，感悟光年之美",
                        Price = 19,
                        ImageUrl = "",
                        CategoryId = 1,
                        ProductType = "Science"
                    },
                    new Product
                    {
                        ProductName = "三体2",
                        ProductDescription = "大刘地球往事三部典之黑暗森林",
                        Price = 29,
                        ImageUrl = "",
                        CategoryId = 1,
                        ProductType = "Science fiction"
                    },
                    new Product
                    {
                        ProductName = "特价衣服100件",
                        ProductDescription = "特价便宜",
                        Price = 1029,
                        ImageUrl = "",
                        CategoryId = 2,
                        ProductType = ""
                    }
                );

                context.User.AddRange(
                    new User
                    {
                        UserName = "Admin",
                        Password = "password"
                    },
                    new User
                    {
                        UserName = "guest",
                        Password = "guest"
                    }
                    );
                context.Category.AddRange(
                    new Category
                    {
                        CategoryName = "Books"
                    },
                    new Category
                    {
                        CategoryName = "Clothes"
                     }
                    );
                context.SaveChanges();
            }
        }
    }
}
